package com.image.controller;

import com.common.VO.PicUploadResult;
import com.image.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Elias
 * @version 1.0
 * @date 2021-06-24 10:12
 * @description
 * @copyright 2021
 */
@RestController
@RequestMapping("/img")
public class ImgController {
  @Autowired private ImageService imageService;

  @RequestMapping("/upload")
  @ResponseStatus(HttpStatus.CREATED)
  public PicUploadResult upload(MultipartFile multipartFile) {
    PicUploadResult pic = new PicUploadResult();
    try {
      String url = imageService.uploadImage(multipartFile);
      pic.setUrl(url);
      pic.setError(0);
      return pic;
    } catch (Exception e) {
      e.printStackTrace();
      pic.setError(1);
      pic.setUrl(e.getMessage());
      return pic;
    }
  }

  @RequestMapping("/valistr")
  public void generateVali(@RequestParam("token") String token, HttpServletResponse response) {
    try {
      imageService.generateValistr(token, response.getOutputStream());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @RequestMapping("/delete/valistr")
  public void delVali(@RequestParam("token") String token) {
    try {
      imageService.deleteValistr(token);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
